<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button round type="primary" size="medium" @click="handleCreate">新增</el-button>
      <el-input v-model="query.keyword" placeholder="请输入标签名进行搜索" style="width: 200px;margin-left: 10px;"
                class="filter-item"
                @keyup.enter.native="handleFilter"/>
      <el-button size="medium" v-waves class="filter-item" style="margin-left: 10px;" type="primary"
                 icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
    </div>

    <el-table
      style="margin-top: 20px;"
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" width="100">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="标签名" align="center">
        <template slot-scope="{row}">
          {{ row.name }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="{row}">
          <span>{{ row.createAt | dayFormatTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center">
        <template slot-scope="{row}">
          {{ row.updateAt | dayFormatTime }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="{row,$index}">
          <el-button-group>
            <el-button type="primary" round size="small" @click="handleUpdate(row)">编辑</el-button>
            <el-button type="success" size="small" @click="handleLook(row)">问卷</el-button>
            <el-button type="danger" round size="small" @click="handleDelete(row,$index)">删除</el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      style="text-align: right"
      v-show="total>0"
      :total="total"
      :page.sync="query.offset"
      :limit.sync="query.limit"
      @pagination="getList"
    />

    <el-dialog :visible.sync="dialogFormVisible" :title="textMap[dialogStatus]">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-width="80px" label-position="left">
        <el-form-item label="标签名" prop="name">
          <el-input v-model="temp.name" placeholder="请输入标签名"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          确认
        </el-button>
      </div>
    </el-dialog>

    <!-- 问卷内容 -->
    <el-dialog :visible.sync="dialogConfigVisible" title="问卷配置">
      <el-form size="mini" ref="dataForm" :rules="rules" :model="temp" label-width="80px" label-position="left">
        <div v-for="item in temp2.Questionnaires" :key="item.id" :style="{'margin-left':`${50*item.lv}px`}">
          <el-form-item v-if="item.title" :label="item.title" label-width="auto-width">
          </el-form-item>
          <el-form-item v-if="item.type===0" :label="item.name" label-width="80px">
            <el-radio label="1" v-for="radio in (item.options || '').split(',')">{{ radio }}
            </el-radio>
          </el-form-item>
          <el-form-item v-if="item.type===1" :label="item.name" label-width="80px">
            <el-checkbox label="1" v-for="checkbox in (item.options || '').split(',')">
              {{ checkbox }}
            </el-checkbox>
          </el-form-item>
          <el-form-item v-if="item.type===2" :label="item.name" label-width="80px">
            <el-input style="width: 200px;"></el-input>
          </el-form-item>
        </div>
        <div v-if="temp2.Questionnaires && temp2.Questionnaires.length<=0">
          <span>暂无问卷配置</span>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { fetchList, create, fetchDetail, remove, update } from '@/api/config'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import waves from '@/directive/waves' // waves directive

export default {
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      total: 0,
      query: {
        limit: 10,
        offset: 1,
        keyword: ''
      },
      textMap: {
        update: '编辑',
        create: '新增'
      },
      dialogFormVisible: false,
      dialogConfigVisible: false,
      dialogStatus: '',
      list: null,
      listLoading: true,
      temp: {
        labelId: '',
        name: ''
      },
      temp2: {},
      rules: {
        name: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleFilter() {
      this.query.offset = 1
      this.getList()
    },
    getList() {
      this.listLoading = true
      fetchList(this.query).then(response => {
        this.list = response.data.list
        // this.list = response.data.list.map(it => {
        //   return { ...it, Questionnaires: it.Questionnaires.reverse() }
        // })
        this.total = response.data.total
        this.listLoading = false
      })
    },
    resetTemp() {
      this.temp = {
        labelId: undefined,
        name: ''
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      if (this.$refs['dataForm']) {
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      }
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          create(this.temp).then(() => {
            this.getList()
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '添加成功',
              type: 'success',
              duration: 1000
            })
          })
        }
      })
    },
    // 查看问卷
    handleLook(row) {
      this.temp2 = Object.assign({}, row) // copy obj
      this.dialogConfigVisible = true
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      if (this.$refs['dataForm']) {
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      }
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          update(tempData).then(() => {
            this.getList()
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '修改成功',
              type: 'success',
              duration: 1000
            })
          })
        }
      })
    },
    handleDelete(row, index) {
      this.$confirm('确定删除吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          remove(row.id).then(res => {
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 1000
            })
            this.getList()
          })
        })
        .catch(err => {
          console.error(err)
        })
    }
  }
}
</script>
